/**
 * 政策详情分页js
 * 
 */

var videoCode = 'appvideo';
var pageNum = 1;
var itemNum = 20;
var insretPolicy = document.getElementById("insretPolicy");
var countNum = 0;
var token = "";
var status = "全部";
var pageflag = "";

(function($) {

	mui.init({
		pullRefresh: {
			container: '#content',
			down: {
				style: 'circle',
				callback: pulldownRefresh
			},
			up: {
				auto: true,
				contentrefresh: '正在加载...',
				callback: pullupRefresh
			}
		}
	});
	setPxPerRem();
	mui.plusReady(function() {
		pageflag = getQueryString("pageflag");
	});

})(mui);

//下拉刷新
var isRefresh = false;

function pulldownRefresh() {
	removeLi("policyList");
	isRefresh = true;
	pageNum = 1;
	if(isHaveNet()) {
		getPoilcyList();
	} else {
		mui.toast("无网络连接！");
	}
}
mui("#typeTab").on("tap", "span", function() {

	if(!this.classList.contains("tab-active")) {
		$("#typeTab span").attr("class", "");
		addClass("tab-active", this);
		status = this.id;
	} else {
		$("#typeTab span").attr("class", "");
		status = "全部";
	}
	pulldownRefresh();
});

//上拉加载
var isLoad = false;

function pullupRefresh() {
	isLoad = true;

	if(isHaveNet()) {
		getPoilcyList();
	} else {
		mui.toast("无网络连接！");
	}
}
var total = 0;

function getPoilcyList() {
	var token = localStorage.getItem("pid");
	var pjson = {
		status: status,
		begin: itemNum,
		pid: token,
		pageIndex: pageNum
	};

	var jsonData = {
		op: "GetCarInfos",
		pjson: JSON.stringify(pjson)
	}

	checkParams(jsonData);

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}

	crossDomainAjax(opts, function(data) {

		if(!isEmpty(data) && data.length > 0) {
			var rows = data;
			total = data[0].SumCount;
			countNum = total / itemNum;
			if(!isEmpty(rows)) {
				for(var i = 0; i < rows.length; i++) {
					createPolicyItem(rows[i]);
				}
			} else {
				mui.toast("获取数据失败！");
			}
		} else {
			mui('#content').pullRefresh().endPullup(true);
			mui.toast("获取数据失败！");
		}

		if(isRefresh) {
			mui('#content').pullRefresh().endPulldown();
			isRefresh = false;
		}

		if(isLoad) {
			isLoad = false;
			mui('#content').pullRefresh().endPullup((pageNum > countNum));
		}
		plus.nativeUI.closeWaiting();
		if(pageNum == 1) {
			showEle(content);
			mui('#content').pullRefresh().endPullup(false);
		}
		pageNum++;

	});
}

mui("#policyList").on('tap', 'li', function() {

	var positionPoint = this.getElementsByTagName("div")[0].innerHTML;
	var carNo = this.getElementsByTagName("span")[0].innerHTML;
	var tel = this.getElementsByTagName("span")[1].innerHTML;
	var postdata = {
		pageflag: "page_carlist.html",
		itemID: this.id,
		code: positionPoint,
		carNo: carNo,
		tel: tel
	}
	var change = {
		pageUrl: "page_carinfo.html",
		postData: postdata,
		aniShow: "fade-in"
	}
	pageChange(change);

});

/**
 * 创建政策列表
 * @param {Object} row
 */
function createPolicyItem(row) {
	//	var isHidden = "mui-hidden";
	//	if(!isEmpty(row.status) && row.status == "0") {
	//		isHidden = "";
	//	}
	var createLi = document.createElement("li");
	createLi.className = "mui-table-view-cell";
	createLi.id = row.SOM_CarStatus;
	createLi.innerHTML = '<div>' + row.SOM_Code + '</div>' +
		'<p>车辆信息：<span>' + row.CarNo + '</span></p>' +
		'<p>订单状态：<span>' + row.SOM_CarStatus + '</span></p>' +
		'<p>订单号：<span>' + row.SOM_TiDanCode + '</span></p>';
	document.body.appendChild(createLi);
	insertBefore(createLi, insretPolicy);
}

window.addEventListener('backPullupRefresh', function(event) {
	pulldownRefresh();
}, false);